/**
 * Copyright 2020 Inc.
 **/
package com.myz.alibaba.nacos.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * nacos作为注册中心
 *
 * @author maoyz0621 on 20-3-29
 * @version v1.0
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerDiscovery {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerDiscovery.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Value("${service-url.nacos-server}")
    private String serverUrl;

    @Autowired
    private RestTemplate restTemplate;

    @RestController
    public class EchoController {

        @GetMapping(value = "/consumer/echo/{string}")
        public String echo(@PathVariable String string) {
            return restTemplate.getForObject(serverUrl + "/echo/" + string, String.class);
        }
    }
}
